package org.dromara.xhlj.crud.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.service.xhlj.XhljWonderfulImageTranslationService;
    import org.dromara.common.mybatis.core.page.TableDataInfo;
    import org.dromara.common.mybatis.core.page.PageQuery;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.xhlj.domain.bo.XhljWonderfulImageBo;
import org.dromara.xhlj.domain.vo.XhljWonderfulImageVo;
import org.dromara.xhlj.domain.XhljWonderfulImage;
import org.dromara.xhlj.mapper.XhljWonderfulImageMapper;
import org.dromara.xhlj.crud.service.IXhljWonderfulImageCrudService;
import org.dromara.xhlj.domain.query.XhljWonderfulImageQuery;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.HashMap;
/**
 * 精彩图片Service业务层处理
 *
 * @author yf
 * @date 2024-07-28
 */
@RequiredArgsConstructor
@Service
public class XhljWonderfulImageCrudServiceImpl implements IXhljWonderfulImageCrudService, XhljWonderfulImageTranslationService {

    private final XhljWonderfulImageMapper baseMapper;

    /**
     * 查询精彩图片
     *
     * @param wonderfulImageId 主键
     * @return 精彩图片
     */
    @Override
    public XhljWonderfulImageVo queryById(Integer wonderfulImageId){
        return baseMapper.selectVoById(wonderfulImageId);
    }


    /**
     * 分页查询精彩图片列表
     *
     * @param query        查询条件
     * @param pageQuery 分页参数
     * @return 精彩图片分页列表
     */
     @Override
     public TableDataInfo<XhljWonderfulImageVo> queryPageList(XhljWonderfulImageQuery query, PageQuery pageQuery) {
        LambdaQueryWrapper<XhljWonderfulImage> lqw = buildQueryWrapper(query);
        Page<XhljWonderfulImageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
     }


    /**
     * 查询符合条件的精彩图片列表
     *
     * @param query 查询条件
     * @return 精彩图片列表
     */
    @Override
    public List<XhljWonderfulImageVo> queryList(XhljWonderfulImageQuery query) {
        LambdaQueryWrapper<XhljWonderfulImage> lqw = buildQueryWrapper(query);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<XhljWonderfulImage> buildQueryWrapper(XhljWonderfulImageQuery query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<XhljWonderfulImage> lqw = Wrappers.lambdaQuery();
                    lqw.eq(query.getWonderfulImageId() != null, XhljWonderfulImage::getWonderfulImageId, query.getWonderfulImageId());
                    lqw.eq(query.getConferenceInfoId() != null, XhljWonderfulImage::getConferenceInfoId, query.getConferenceInfoId());
                    lqw.eq(query.getCreateBy() != null, XhljWonderfulImage::getCreateBy, query.getCreateBy());
        return lqw;
    }



    /**
     * 新增精彩图片
     *
     * @param bo 精彩图片
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(XhljWonderfulImageBo bo) {
        XhljWonderfulImage add = MapstructUtils.convert(bo, XhljWonderfulImage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setWonderfulImageId(add.getWonderfulImageId());
        }
        return flag;
    }

    /**
     * 修改精彩图片
     *
     * @param bo 精彩图片
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(XhljWonderfulImageBo bo) {
        XhljWonderfulImage update = MapstructUtils.convert(bo, XhljWonderfulImage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(XhljWonderfulImage entity){
        //SAIL 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除精彩图片信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //SAIL 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 翻译 精彩图片 List
     *
     * @param id 外键
     * @return xhljWonderfulImageVoList
     */
    @Override
    @Cacheable(cacheNames = XhljWonderfulImage.TRANS_ID_TO_VO_LIST, key = "#id")
    public Object selectVoListById(Integer id,String fieldName){
         Map<String, Object> query = new HashMap<>();
         query.put(fieldName, id);
         return baseMapper.selectVoByMap(query);
    }

    /**
     * 翻译 精彩图片
     *
     * @param id 主键
     * @return xhljWonderfulImage
     */
    @Override
    @Cacheable(cacheNames = XhljWonderfulImage.TRANS_ID_TO_VO, key = "#id")
    public Object selectVoById(Integer id){
        return baseMapper.selectVoById(id);
    }
}
